import { getData, addData, updateData } from '@vimi/utils-api'
import { nzhToMoney, arrayFromString, dateFormat } from '@vimi/utils-tool'
import { getApprove } from '@/credit/mixin/wf'
import store from '@/bigcredit/store'

const loanTableName = 'loan_approval'
const attachTableName = 'datum_customer_attachment'
const noticeTableName = 'loan_notice'
const SERVICE_NAME = 'credit'
export const wfKey = 'LOAD_APPROVAL_DIR_QUICK'
export const getLoanData = queryId =>
  getData({
    table: loanTableName,
    params: {
      'customer_related_id.eq': queryId,
    },
  }).then(res => res?.data?.[0] ?? {})

export const getCustomerNotice = queryId =>
  getData({
    table: noticeTableName,
    params: {
      'customer_related_id.eq': queryId,
    },
  }).then(res => res?.data?.[0] ?? {})

export const updateLoanApproval = (id, data) => updateData(SERVICE_NAME, loanTableName, id, data)

export const addLoanApproval = data => addData(SERVICE_NAME, loanTableName, data)

export const getAttachment = (relatedId, attachName = []) =>
  getData(
    SERVICE_NAME,
    attachTableName,
    {
      'file_type.eq': 1,
      'customer_related_id.eq': relatedId,
      'attachment_type.in': attachName.join(','),
    },
    { create_time: 'asc' }
  ).then(res => {
    const attachment = []
    const data = res?.data ?? []
    data.forEach(item => {
      const images = arrayFromString(item.attachment).map(img => {
        return {
          url: img,
          id: item.id,
          attachment_type: item.attachment_type,
          remark: item.remark,
          name: item.remark || item.attachment_type,
          attachment: img,
        }
      })
      attachment.push(...images)
    })
    return attachment
  })

export const sendNotice = (related, id) => {
  const { entrustContract, clientId } = store.getters
  const isPerson = related.customer_type === 2
  const data = {
    customer_related_id: related.customer_related_id,
    gua_org_id: related.recommend_service,
    bank_name: related.bank_name,
    entrust_gua_no: entrustContract.entrust_contract_cno,
    loan_contract_name: '借款合同',
    loan_contract_no: entrustContract.loan_contract_no,
    ensure_contract_name: '保证合同',
    ensure_contract_no: entrustContract.ensure_contract_no,
    customer_name: related.customer_name,
    apply_funds: entrustContract.reply_funds,
    apply_funds_capital: nzhToMoney(Math.round(entrustContract.reply_funds * 10000)),
    load_years: entrustContract.load_years * 12,
    sign_date: dateFormat(new Date(), 'yyyy年MM月dd日'),
    cert_no: isPerson ? related.lender_idcard : related.register_no,
    service_id: clientId,
  }

  return id
    ? updateData(SERVICE_NAME, noticeTableName, id, data)
    : addData(SERVICE_NAME, noticeTableName, data)
}

const approveConfig = {
  // 'LOAD_APPROVAL_DIR-RESA': {
  //   cmpt: 'approve-res',
  //   rekey: null,
  // },
  // 'LOAD_APPROVAL_DIR-RISK_MANAGE': {
  //   cmpt: 'approve-common',
  //   rekey: 'LOAD_APPROVAL_DIR-RESA',
  // },
}

// 获取审批流程配置信息
export const getApproveProcess = queryId =>
  getApprove({
    wfKey,
    queryId,
    config: approveConfig,
  })
